//
//  UIImage+YSFromColor.m
//  YSKitDemo
//
//  Created by MOLBASE on 2018/8/9.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "UIImage+YSFromColor.h"

@implementation UIImage (YSFromColor)

+ (instancetype)ys_imageWithGradientColors:(nonnull NSArray<UIColor *> *)colors direction:(YSGradientDirection)direction {
    return [self ys_imageWithGradientColors:colors direction:direction size:CGSizeMake(1.f, 1.f)];
}

+ (instancetype)ys_imageWithGradientColors:(nonnull NSArray<UIColor *> *)colors direction:(YSGradientDirection)direction size:(CGSize)size {
    if (colors.count == 0) {
        return nil;
    }
    
    if (colors.count == 1) {
        return [self ys_imageWithColor:colors.firstObject size:size];
    }
    
    NSMutableArray *cgcolors = [NSMutableArray arrayWithCapacity:colors.count];
    for (UIColor *color in colors) {
        [cgcolors addObject:(id)color.CGColor];
    }
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)cgcolors, NULL);    // 渐变颜色效果设置
    
    CGPoint startPoint, endPoint;
    switch (direction) {
        case YSGradientDirectionLeft:
            startPoint = CGPointMake(size.width, size.height / 2);
            endPoint   = CGPointMake(0,          size.height / 2);
            break;
        case YSGradientDirectionRight:
            startPoint = CGPointMake(0,          size.height / 2);
            endPoint   = CGPointMake(size.width, size.height / 2);
            break;
        case YSGradientDirectionTop:
            startPoint = CGPointMake(size.width / 2, size.height);
            endPoint   = CGPointMake(size.width / 2, 0);
            break;
        case YSGradientDirectionBottom:
            startPoint = CGPointMake(size.width / 2, 0);
            endPoint   = CGPointMake(size.width / 2, size.height);
            break;
        case YSGradientDirectionTopLeft:
            startPoint = CGPointMake(size.width, size.height);
            endPoint   = CGPointMake(0,          0);
            break;
        case YSGradientDirectionTopRight:
            startPoint = CGPointMake(0,          size.height);
            endPoint   = CGPointMake(size.width, 0);
            break;
        case YSGradientDirectionBottomLeft:
            startPoint = CGPointMake(size.width, 0);
            endPoint   = CGPointMake(0,          size.height);
            break;
        case YSGradientDirectionBottomRight:
            startPoint = CGPointMake(0,          0);
            endPoint   = CGPointMake(size.width, size.height);
            break;
        default: // 默认向右
            startPoint = CGPointMake(0,          size.height / 2);
            endPoint   = CGPointMake(size.width, size.height / 2);
            break;
    }
    
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    return image;
}

+ (instancetype)ys_imageWithColor:(nonnull UIColor *)color {
    return [self ys_imageWithColor:color size:CGSizeMake(1.f, 1.f)];
}

+ (instancetype)ys_imageWithColor:(nonnull UIColor *)color size:(CGSize)size {
    CGRect rect = (CGRect){CGPointZero, size};
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
